{include file="public/head"/}
<div class="layui-fluid" id="LAY-app-message">
    <div class="layui-card">
        <div class="layui-card-header">字段列表</div>
        <input type="hidden" id="mid" value="{:input('mid')}">
        <div class="layui-card-body">
            <div class="clt-auth-btns" style="margin-bottom: 10px;">
                <button class="layui-btn layui-btn-sm" data-type="add"><i class="layui-icon layui-icon-add-1"></i>添加字段</button>
            </div>
            <table class="layui-table" id="list" lay-filter="list"></table>
        </div>
    </div>
</div>
{include file="public/foot"/}
<script type="text/html" id="isSystem">
    <input type="checkbox" name="issystem" disabled value="{{d.id}}" lay-skin="switch" lay-text="是|否" lay-filter="status" {{ d.issystem == 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="required">
    <input type="checkbox" name="required" value="{{d.id}}" lay-skin="switch" lay-text="是|否" lay-filter="requiredStatus" {{ d.required == 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="order">
    <input name="{{d.id}}" data-id="{{d.id}}" class="list_order layui-input" style="height: 25px" value="{{d.sort}}" size="10"/>
</script>

<script type="text/html" id="action">
    {{# if(d.disable==1){ }}
    <button class="layui-btn layui-btn-xs layui-btn-danger" lay-event="doStatus">已禁用</button>
    {{# }else if(d.disable==2){ }}
    <button class="layui-btn layui-btn-xs layui-btn-disabled">已禁用</button>
    {{# }else{ }}
    <button class="layui-btn layui-btn-xs layui-btn-warm" lay-event="doStatus">已启用</button>
    {{# } }}
    <button data-mid="{{d.moduleid}}" class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
    {{# if(d.delStatus==1){ }}
    <button class="layui-btn layui-btn-xs layui-btn-disabled"><i class="layui-icon layui-icon-delete"></i>删除</button>
    {{# }else{ }}
    <button class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</button>
    {{# } }}
</script>
<script>
    layui.config({
        base: '__LAYDMIN__/',{if config('app.app_debug')}version: true{/if}
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index','table','form'],function(){
			var $ = layui.$, admin = layui.admin, table = layui.table, element = layui.element,form = layui.form,mid = $('#mid').val();
			var tableLoadDiv = window.top.layer.load(0,{shade:[0.15, '#000']}); //加载层,table.render完成时需要关闭
			//全部数据
			tableIn = table.render({
				elem: '#list',
				url: '{:url(SYS_PATH."/field/index")}',
				where:{mid:mid},
				method: 'post',
				cols: [[
					{field: 'field', title: '字段名', width: 120,fixed: true},
					{field: 'name', title: '别名', width: 100},
					{field: 'type', title: '字段类型', width: 100},
					{field: 'issystem', align: 'center', title: '系统字段', width: 100, toolbar: '#isSystem'},
					{field: 'required', align: 'center', title: '必填', width: 80, toolbar: '#required'},
					{field: 'sort', title: '排序', width: 100, sort: true,toolbar: '#order'},
					{width: 220, align: 'center', toolbar: '#action',"title":'操作'}
				]],
				done:function (res) {   //返回数据执行回调函数
						window.top.layer.close(tableLoadDiv);    //返回数据关闭loading
				}				
			});	
			
			$('.clt-auth-btns .layui-btn').on('click', function(){
				layui.layerOpen("{:url(SYS_PATH.'/field/add')}?mid="+mid);
			});			
			
			//列表监听
			table.on('tool(list)', function(obj){
					var data = obj.data;
					var id = data.id;					
					switch(obj.event){
						case 'doStatus':
								admin.req({
									url: '{:url(SYS_PATH."/field/doStatus")}'
									, type: 'post'
									, data: {id:id}
									, done: function (res) {
										window.top.layer.msg(res.msg, {icon: 1, time: 1000,shade:[0.5,'#000']}, function () {
											tableIn.reload();
										});
									}
								});
							break;
						case 'edit':
							layui.layerOpen("{:url(SYS_PATH.'/field/edit')}?id="+id+'&mid='+mid,{type:'edit'});
							break;
						case 'del':
							var uri="{:url(SYS_PATH.'/field/doDelete')}"
							layui.doDelete(uri,"确定要删除该字段吗？",id,obj);//单个删除接口						
							break;
					}
			});	
			
			//列表监听是否必填
			form.on('switch(requiredStatus)', function (obj) {
					var id = this.value;
					var required = obj.elem.checked === true ? 1 : 0;
					admin.req({
						url: '{:url(SYS_PATH.'/field/requiredChange')}',
						type:'post',
						data: {'id': id, 'required': required},
						success: function (res) {
							if (res.code == 0) {
								window.top.layer.msg(res.msg, {time: 1000, icon: 1,shade:[0.5,'#000']});
							} else {
								window.top.layer.msg(res.msg, {time: 1000, icon: 2,shade:[0.5,'#000']});
								if (obj.elem.checked == true) {
									$(obj.elem).prop('checked', false);
								} else {
									$(obj.elem).prop('checked', true);
								}
								form.render();
							}
						}
					});
		});					
			
			
				
	});
</script>
</body>
</html>